package com.tangji.signaltower.exception;

import com.fqgj.exception.common.ApplicationException;
import com.tangji.signaltower.bean.ApiResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

/**
 * Created by zcg on 15/10/5.
 */
@ControllerAdvice
public class ErrorHandler {
    private static final Logger logger = LoggerFactory.getLogger(ErrorHandler.class);

    @ExceptionHandler(Exception.class)
    @ResponseStatus(value = HttpStatus.OK)
    @ResponseBody
    public ApiResponse handleAllException(Exception ex) {
        logger.info("signal_tower统一报错日志", ex);
        ApiResponse response = new ApiResponse<>();
        if (ex instanceof ApplicationException) {
            ApplicationException error = (ApplicationException) ex;
            response.setCode(500);
            response.setMsg("Error:" + error.getMessage());
        } else {
            response.setCode(500);
            response.setMsg("Error");
        }
        return response;
    }
}

